package com.logos.ruralrevitalization.service.impl;

import com.logos.ruralrevitalization.converter.AttractionReviewConverter;
import com.logos.ruralrevitalization.entity.AttractionReviews;
import com.logos.ruralrevitalization.entity.TouristAttractions;
import com.logos.ruralrevitalization.enums.IsDeleteFlagEnum;
import com.logos.ruralrevitalization.exception.ReviewException;
import com.logos.ruralrevitalization.mapper.AttractionReviewsMapper;
import com.logos.ruralrevitalization.mapper.TouristAttractionsMapper;
import com.logos.ruralrevitalization.req.AttractionReviewsReq;
import com.logos.ruralrevitalization.resp.AttractionReviewsResp;
import com.logos.ruralrevitalization.service.AttractionReviewsService;
import com.logos.ruralrevitalization.utils.TouristLoginUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;

/**
 * 景点评论表(AttractionReviews)表服务实现类
 *
 * @author rose
 * @since 2025-03-25 21:58:34
 */
@Service("attractionReviewsService")
public class AttractionReviewsServiceImpl implements AttractionReviewsService {
    @Resource
    private AttractionReviewsMapper attractionReviewsMapper;

    @Resource
    private TouristAttractionsMapper touristAttractionsMapper;

    @Resource
    private TouristLoginUtil touristLoginUtil;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public AttractionReviews queryById(Integer id) {
        return this.attractionReviewsMapper.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param attractionReviews 筛选条件
     * @param pageRequest       分页对象
     * @return 查询结果
     */
    @Override
    public Page<AttractionReviews> queryByPage(AttractionReviews attractionReviews, PageRequest pageRequest) {
        long total = this.attractionReviewsMapper.count(attractionReviews);
        return new PageImpl<>(this.attractionReviewsMapper.queryAllByLimit(attractionReviews, pageRequest), pageRequest, total);
    }


    /**
     * 修改数据
     *
     * @param attractionReviews 实例对象
     * @return 实例对象
     */
    @Override
    public AttractionReviews update(AttractionReviews attractionReviews) {
        this.attractionReviewsMapper.update(attractionReviews);
        return this.queryById(attractionReviews.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.attractionReviewsMapper.deleteById(id) > 0;
    }

    /**
     * 发表评论
     */
    @Override
    public Boolean insert(AttractionReviewsReq attractionReviewsReq) {
        // 每个人只能评论一次
        AttractionReviews attractionReviews = new AttractionReviews();
        attractionReviews.setIsDeleted(IsDeleteFlagEnum.UN_DELETED.getCode());
        attractionReviews.setAttractionId(attractionReviewsReq.getAttractionId());
        attractionReviews.setUserId(attractionReviewsReq.getUserId());
        List<AttractionReviewsResp> attractionReviewsRespList = attractionReviewsMapper.queryByCondition(attractionReviews);
        if (!attractionReviewsRespList.isEmpty()) {
            throw new ReviewException("您已经评价过该景点了");
        }
        // 更新景点评分
        // 拿到当前景点的评分
        TouristAttractions touristAttractions = touristAttractionsMapper.queryById(attractionReviewsReq.getAttractionId());
        Double totalRating = touristAttractions.getRating();
        // 查询与当前景点评论的条数
        Integer totalCount = attractionReviewsMapper.countReview(attractionReviewsReq.getAttractionId());
        Double curRating = attractionReviewsReq.getRating();
        if (totalCount == 0) {
            totalRating = curRating;
        } else {
            totalRating = (totalRating * totalCount + curRating) / (totalCount + 1);
        }
        touristAttractions.setRating(totalRating);
        touristAttractionsMapper.update(touristAttractions);
        // 组装数据
        attractionReviews = AttractionReviewConverter.INSTANCE.convertReqToEntity(attractionReviewsReq);
        // 新增一条评价
        return attractionReviewsMapper.insert(attractionReviews) > 0;
    }

    /**
     * 查询所有评论
     */
    @Override
    public List<AttractionReviewsResp> queryByAttractionId(Integer attractionId) {
        AttractionReviews attractionReviews = new AttractionReviews();
        attractionReviews.setAttractionId(attractionId);
        attractionReviews.setIsDeleted(IsDeleteFlagEnum.UN_DELETED.getCode());
        List<AttractionReviewsResp> attractionReviewsRespList = attractionReviewsMapper.queryByCondition(attractionReviews);
        return attractionReviewsRespList;
    }

    @Override
    public Boolean updateById(AttractionReviewsReq attractionReviewsReq) {
        AttractionReviews attractionReviews = attractionReviewsMapper.queryById(attractionReviewsReq.getId());
        // 删除该评论的同时将该景点的评分重新计算
        // 拿到当前景点的评分
        TouristAttractions touristAttractions = touristAttractionsMapper.queryById(attractionReviews.getAttractionId());
        Double totalRating = touristAttractions.getRating();
        // 查询与当前景点评论的条数
        Integer totalCount = attractionReviewsMapper.countReview(attractionReviews.getAttractionId());
        Double curRating = attractionReviews.getRating();
        if (totalCount == 1) {
            totalRating = 0.0;
        } else {
            totalRating = (totalRating * totalCount - curRating) / (totalCount - 1);
        }
        touristAttractions.setRating(totalRating);
        touristAttractionsMapper.update(touristAttractions);
        // 删除该条评论
        attractionReviews.setIsDeleted(IsDeleteFlagEnum.DELETED.getCode());
        return attractionReviewsMapper.update(attractionReviews) > 0;
    }
}
